package org.lex.expression;


public class Lexer {
	// delimiter�еĿո��ڷָ�id�������ֵ�ʱ������
	private static final String DELIMITER = " +-*/%^();";

	private static final int EOF = -1;

	private MyStringReader reader = null;
	private int lookahead = EOF;

	public Lexer() {
		this("");
	}

	public Lexer(String source) {
		this.setSource(source);
	}

	public void setSource(String source) {
		if (null == this.reader)
			this.reader = new MyStringReader(source);
		else
			this.reader.setSource(source);
	}

	public Token nextToken() {
		lookahead = reader.read();
		// skip whitespaces
		while (Character.isWhitespace(lookahead))
			lookahead = reader.read();

		// handle END
		if (lookahead <= 0)
			return Token.END;

		// handle operator and parentheses
		if (true == Lexer.isDelimiter(lookahead))
			return Token.parseOperatorToken((char) lookahead);

		// handle numeric value
		if (true == Character.isDigit(lookahead)) {
			StringBuilder buffer = new StringBuilder(16);
			do {
				buffer.append((char) lookahead);
				lookahead = reader.read();
			} while (lookahead >= 0 && false == isDelimiter(lookahead));
			this.reader.pushback(lookahead);
			return new Token(TokenType.NUMERIC, buffer.toString());
		}
		return Token.UNKOWN;
	}

	protected static boolean isDelimiter(int ch) {
		return Lexer.DELIMITER.indexOf(ch) != -1;
	}
}
